package com.chinaoly.myapplication;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chen.helper.LightRecycleViewHelper;
import com.chen.helper.widget.RefreshRecycleView;
import com.chen.helper.widget.SimpleDividerDecoration;
import com.cj.databse.DataBaseManager;
import com.cj.databse.PersonBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RvLoadMoreActivity extends AppCompatActivity {

    TextView btn1;
    TextView btn2;
    TextView btn3;
    TextView btn4;
    final int NORMAL = 17;
    final int ABNORMAL = 8;
    int total;

    private RefreshRecycleView recycleView;
    private RecyclerView recycleViewTotal;
    LightRecycleViewHelper<PersonBean> helper;
    LightRecycleViewHelper<PersonBean> helperTotal;
    final MutableLiveData<List<PersonBean>> mutableLiveData = new MutableLiveData<>();
    private Map<String, String> map = new HashMap<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_load_more);
        recycleViewTotal = findViewById(R.id.rv_total);
        recycleView = findViewById(R.id.rrv);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        initRv();


        dataObserver();
        requestDataListener();
        openDatabase();
        btn1();
        btn2();
        btn3();
        btn4();

    }

    private void dataObserver() {
        mutableLiveData.observe(this, new Observer<List<PersonBean>>() {
            @Override
            public void onChanged(@Nullable List<PersonBean> personBeans) {
                helper.setListData(personBeans);
            }
        });
    }

    private void requestDataListener() {
        helper.postData(new Runnable() {
            @Override
            public void run() {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        map.put(DataBaseManager.PAGE_NUM, helper.getPageNum() + "");
                        final List<PersonBean> person = DataBaseManager.getManager(getApplication()).getPerson(map);
                        helper.setLastPageNum(total);
                        /*try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }*/
                        mutableLiveData.postValue(person);
                    }
                }.start();
            }
        });
    }

    private void openDatabase() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                DataBaseManager.getManager(getApplication()).open();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), "创建完成", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();

    }

    private void btn3() {
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        List<PersonBean> pList = new ArrayList<>();
                        for (int i = 0; i < NORMAL; i++) {
                            PersonBean p = new PersonBean();
                            p.setAge(i + 1);
                            p.setName("zs" + i);
                            p.setAddress("Add" + i);
                            p.setNormal(true);
                            pList.add(p);
                        }
                        for (int i = NORMAL; i < ABNORMAL + NORMAL; i++) {
                            PersonBean p = new PersonBean();
                            p.setAge(i + 1);
                            p.setName("zs" + i);
                            p.setAddress("Add" + i);
                            p.setNormal(false);
                            pList.add(p);
                        }
                        DataBaseManager.getManager(getApplication()).clearAllTables();
                        DataBaseManager.getManager(getApplication()).addPerson(pList);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getBaseContext(), "创建数据完成", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                }.start();
            }
        });
    }


    private void btn1() {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                map.clear();
                map.put(DataBaseManager.PAGE_NUM, helper.getPageNum() + "");
                total = ABNORMAL + NORMAL;
                helper.requestFirstPage();
            }
        });
    }

    private void btn2() {
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                total = ABNORMAL;
                map.clear();
                // helper.resetParam();
                helper.requestFirstPage();
                map.put(DataBaseManager.PAGE_NUM, helper.getPageNum() + "");
                map.put(DataBaseManager.NORMAL, false + "");
            }
        });
    }

    private void initRv() {
        RecyclerView.RecycledViewPool pool = recycleView.getRecyclerView().getRecycledViewPool();
        recycleViewTotal.setRecycledViewPool(pool);
        recycleView.getRecyclerView().addItemDecoration(new SimpleDividerDecoration(this));
        helper = new LightRecycleViewHelper<PersonBean>(R.layout.item_person, recycleView) {
            @Override
            protected void setItemData(BaseViewHolder helper, PersonBean item) {
                itemBind(helper, item);
            }
        };
        helper.setFirstPageNum(0);

        helperTotal = new LightRecycleViewHelper<PersonBean>(R.layout.item_person, recycleViewTotal) {
            @Override
            protected void setItemData(BaseViewHolder helper, PersonBean item) {
                itemBind(helper, item);
            }
        };
    }

    private void itemBind(BaseViewHolder helper, PersonBean item) {
        helper.setText(R.id.tv_name, item.getName())
                .setText(R.id.tv_address, item.getAddress())
                .setText(R.id.tv_age, item.getAge() + "")
                .setTextColor(R.id.tv_name, ContextCompat.getColor(getBaseContext(), item.isNormal() ? R.color.blue : R.color.red))
                .setTextColor(R.id.tv_address, ContextCompat.getColor(getBaseContext(), item.isNormal() ? R.color.blue : R.color.red))
                .setTextColor(R.id.tv_age, ContextCompat.getColor(getBaseContext(), item.isNormal() ? R.color.blue : R.color.red))

                .setImageResource(R.id.iv_normal, item.isNormal()
                        ? R.drawable.ic_android_blue : R.drawable.ic_android_red);
    }

    private void btn4() {
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        final List<PersonBean> person = DataBaseManager.getManager(getApplication()).getAllPerson();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                helperTotal.setListData(person);
                                //Toast.makeText(v.getContext(), "打开数据库", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }.start();

            }
        });
    }

    @Override
    protected void onDestroy() {
        DataBaseManager.getManager(getApplication()).closeDataBase();
        super.onDestroy();
    }
}
